<template>
  <ex_super_dialog_form ref="dialogRef" v-bind="option" v-on="$listeners" @afterSubmitSuccess="afterSubmitSuccess">
    <template slot="dialog_bottom">
      <el-alert style="margin-bottom: 10px;" :title="$t('tip_purchase_syn_order')" type="info" :closable="false"
        show-icon>
      </el-alert>
    </template>
  </ex_super_dialog_form>
</template>

<script>
export default {
  components: {},
  props: {},
  data() {
    return {
      warehouseCode: '',
      option: {
        customClass: "dialog_auto",
        submitDataHandle: (data) => {
          const strArray = data.orderIds.split(/[,，\s\n]/).filter(_ => _)
          return { warehouseCode: this.warehouseCode, orderIds: strArray,thirdPartyAuthorizedId:data.thirdPartyAuthorizedId};
        },
        form: {
          itemClass: "form_item_default",
          inputClass: "form_input_big",
          model: [
            {
              key: "code",
              label: "label_purchase_receiving_warehouse",
              type: 'base_warehouse_select',
              on: {
                changeItem: row => {
                  this.warehouseCode = row.code
                }
              }
            },
            {
              key: "thirdPartyAuthorizedId",
              label: "label_shop_name",
              type: 'select',
              url: '/oms-api/purchase/OmsPurchaseOrderRpc/getThirdPartyAuthorized.json',
              urlType: 'form',
              option: it => ({ label: it.sellerId + `[${it.customerName}]`, value: it.id })
            },
            {
              key: "orderIds",
              label: "label_platform_order_1688",
              render: (h, { data }) => {
                return <el-input type="textarea" value={data.orderIds} onInput={e => this.$set(data, 'orderIds', e)} placeholder={this.$t('tip_purchase_syn_order2')} />
              },
            },
          ],
          rules: {
            thirdPartyAuthorizedId: [{ required: true, message: this.$tt('tip_can_no_empty'), trigger: 'blur' }],
            orderIds: [{ required: true, message: this.$tt('tip_can_no_empty'), trigger: 'blur' }]
          },
        }
      }
    };
  },
  methods: {
    show(data) {
      this["$refs"]["dialogRef"].showNew({
        title: this.$t('label_sync_order_1688'),
        submitUrl: '/oms-api/purchase/OmsPurchaseOrderRpc/sync.json',
        submitCall: (response, formData, submitCallResult) => {

          const h = this.$createElement;
          const failureDetails = []
          response.data.failureDetails.forEach(item => {
            failureDetails.push(h('p', null, item))
          })
          const message = h('div', null, [
            h('p', null, `${this.$t('tip_exe_success')}${response.data.successQty}`),
            h('p', null, `${this.$t('tip_exe_failure')}${response.data.failureQty}`),
            h('br', null),
            h('p', null, this.$t('tip_exe_failure_details')),
            ...failureDetails,
          ])
          this.$msgbox({
            title: this.$tt('label_tip'),
            message: message,
            showCancelButton: false,
            customClass:'customMsgbox'
          })

          if(response.data.failureQty===0){
            submitCallResult(true,true,false)
          }
        },
      });
    },
    afterSubmitSuccess() {
      this.$emit("submit");
    },
  },
  destroyed() { },
};
</script>
